package se19team1e.gui.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import se19team1e.gui.MessageBox;

/**
 * An implementation of IMessageBox that uses real AWT GUI screen to display a message to the user. 
 * 
 * <p>
 * The message box is a modal dialog box with an owning parent window.
 * </p>
 * 
 * @author lai shiaw san
 *
 */
@SuppressWarnings("serial")
public class AWTMessageBox extends Dialog implements MessageBox {

    /**
     * Label to display message in.
     */
    private final Label label;
    
    /**
     * Button to close the message box.
     */
    private final Button okButton;
    
    /**
     * Default constructor
     * 
     * @param parent The owner parent window.
     */
    public AWTMessageBox( Window parent ) {
        super( parent, ModalityType.APPLICATION_MODAL );
        
        // disable resize
        this.setResizable(false);
        
        // use normal border layout container
        this.setLayout( new BorderLayout() );
        
        // create panel, label, ok button
        Panel panel = new Panel();
        // panel uses grid layout stacking the message
        panel.setLayout( new GridLayout( 2, 1 ) );
        this.add( panel, BorderLayout.CENTER );
        
        this.label = new Label();
        panel.add( label );
        this.okButton = new Button("OK");
        panel.add( okButton );
        
        // disable minimize/maximize
        
        // pack the content first time
        this.pack();
        
        // centralize to parent
        this.setLocationRelativeTo( parent );
    }

    /* (non-Javadoc)
     * @see se19team1e.IMessageBox#setMessage(java.lang.String)
     */
    @Override
    public void setMessage( String message ) {
        // set the content
        this.label.setText( message );
        // pack the content
        this.pack();
    }

    /* (non-Javadoc)
     * @see se19team1e.IMessageBox#setActionListener(java.awt.event.ActionListener)
     */
    @Override
    public void setActionListener( ActionListener actionListener ) {
        this.okButton.addActionListener( actionListener );
    }

    /**
     * This entry point is only for testing MessageBox.
     * 
     * @param args Arguments passed to the application.
     */
    @SuppressWarnings("deprecation")
    public static void main( String[] args ) {
        AWTBaseScreen screen = new AWTBaseScreen(null) {
            @Override
            public void updateAvailableActions() {
                // empty
            }};
        screen.show();
        final AWTMessageBox messageBox = new AWTMessageBox(screen);
        messageBox.setTitle("title");
        messageBox.setMessage("Message");
        messageBox.setActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed( ActionEvent e ) {
                messageBox.hide();
                messageBox.dispose();
            }
        });
        messageBox.show();
        screen.dispose();
    }

}
